var AddictTable = new Class({
  options: {
    currentPage : 0,
    lines       : 5,
    sort        : {
			col :  1,
			way   : 'asc'
		},
    filter : {
			col : -1,
			value : '',
		  field : null
		},
		buttons:{}
  },
  
  initialize: function(table, options) {
		var tthis = this;
    this.tableEL = table;
    this.setOptions(options);
    this._init();
    this._sort();
		this.filterAll = 'Tous';
    this._display();
		if (this.options.filter.field){
			this.options.filter.field.addEvent('change', function(event){
				var el = $pick(event.target||event.srcElement);
				tthis.options.filter.value = (el.value==tthis.filterAll)?'':el.value;
				tthis._display();
			});
			this._initSelectFilter();
		}
		
		for (var el in this.options.buttons){
      $(el).addEvent('click', function(event){
				new Event(event).stop();
				tthis[tthis.options.buttons[$pick(event.target||event.srcElement).id]]();
      });
		}
  },
	_initSelectFilter:function(){
		var tthis = this;
		this._removeAllOptions(this.options.filter.field);
		this._addOptionSelect(this.options.filter.field,this.filterAll,true);
		var arrV = [];
    this.elements.each(function(row, index) {
			var value=row.getChildren()[tthis.options.filter.col].innerHTML;
			if (arrV.indexOf(value)==-1){
			  arrV.push(value);	
			}      
		})	 		
    arrV.sort().each(function(el,idx){
		  tthis._addOptionSelect(tthis.options.filter.field,el,(tthis.options.filter.value==el));
		});
	},
	_removeAllOptions : function(elSel){
    for (i = elSel.length - 1; i>=0; i--) {
      if (elSel.options[i].selected) {
        elSel.remove(i);
      }
    }	
	},
  _addOptionSelect : function (elSel,txt,selected){
		var elOptNew      = document.createElement('option');
    elOptNew.text     = txt;
    elOptNew.value    = txt;
    elOptNew.inject(elSel);      
		if (selected){
			elSel.selectedIndex = (elOptNew.index) ;
		} 
	}, 
  _init: function() {
    var tthis = this;
    this.tbody    = this.tableEL.getElement('tbody'); 
    this.elements = this.tbody.getElements('tr');
    this.countPages = Math.ceil(this.elements.length / this.options.lines);
    this.elements.each(function(el){
      el.parentObj = tthis;
      el.findData = function(elem){
        var child = elem.getFirst();
        if(child){
          return el.findData(child);
        }else{
          return elem.innerHTML.trim();
        }
      }
    });
  },
  _compareFunc : function(a,b){
    var1 = a.getChildren()[a.parentObj.options.sort.col].innerHTML;
    var2 = b.getChildren()[b.parentObj.options.sort.col].innerHTML;      
    if (!isNaN(var1) && !isNaN(var1)){
      return (b.parentObj.options.sort.way=='asc'?var2-var1:var1-var2);
    }else{
      return ([var1,var2].sort()[0]==var1)?(b.parentObj.options.sort.way=='asc'?1:-1):(b.parentObj.options.sort.way=='asc'?-1:1);
    }
  },
  _sort : function(){
    this.elements.sort(this._compareFunc);
    this.elements.injectInside(this.tbody);
  },
  _display: function() {
    var count=0;
    var tthis = this;
    this.elements.each(function(row, index) {
      if (tthis.options.filter.col==-1 ||tthis.options.filter.value==''){
       row.addClass('filtered');
      }else{
        if(row.getChildren()[tthis.options.filter.col].innerHTML==tthis.options.filter.value){
          row.addClass('filtered');
        }else{
          row.removeClass('filtered');
          row.setStyle('display', 'none');
        }
      }
    });
    var filteredEl = $$('.filtered',this.elements);
    filteredEl.each(function(row, index) {
      if (index < this.options.lines * this.options.currentPage) {
        row.setStyle('display', 'none');
      } else if (index > this.options.lines * (this.options.currentPage + 1) - 1) {
        row.setStyle('display', 'none');
      } else {
        row.setStyle('display', 'table-row');
        row.removeClass('r1').removeClass('r2');
        row.addClass(count++ % 2 == 0 ? 'r1' : 'r2'); 
      }
    }.bind(this));
    this.countPages = Math.ceil(filteredEl.length / this.options.lines);
  },
  
  goToPage: function(page) {
    if (page < 0 || page > this.countPages - 1) return;
    this.options.currentPage = page;
    this._display();
  },
  
  previousPage: function() {
    this.goToPage(this.options.currentPage - 1);
  },
  
  nextPage: function() {
    this.goToPage(this.options.currentPage + 1);
  },
  
  firstPage: function() {
    this.goToPage(0);
  },
  
  lastPage: function() {
    this.goToPage(this.countPages - 1);
  }
});

AddictTable.implement(new Options);